package edu.ptu.springboot04.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.ptu.springboot04.domain.create.OrderDtlCreate;
import edu.ptu.springboot04.domain.create.ProductOfShoppingCart;
import edu.ptu.springboot04.domain.entity.Orders;
import edu.ptu.springboot04.domain.query.OrderQuery;
import edu.ptu.springboot04.mapper.OrderDtlMapper;
import edu.ptu.springboot04.mapper.OrderMapper;
import edu.ptu.springboot04.mapper.ShoppingCartMapper;
import edu.ptu.springboot04.service.IOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @BelongsProject: springboot04
 * @BelongsPackage: edu.ptu.springboot04.service.impl
 * @Author: HYJ
 * @CreateTime: 2025-03-21  21:16
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements IOrderService {

    //1.根据购物车ids查询所有的商品列表
    //2.根据http获取userId,生成order表
    //3.根据userId查询到的商品列表生成orderDtl表
    //4.删除购物车
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private OrderDtlMapper orderDtlMapper;

    //生成订单
    @Transactional
    @Override
    public int insertOrders(OrderQuery orderQuery, Long userId) {

        String orderNum = UUID.randomUUID().toString().replace("-", "");
        //获取购物车ids
        List<Integer> ids = orderQuery.getShoppingCartIds();

        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setOrderNum(orderNum);

        //生成order表
        orderMapper.insertOrder(orders);

        //根据Ids获取所有的商品
        List<ProductOfShoppingCart> productOfShoppingCartList =
                shoppingCartMapper.listAllByShoppingCartIds(ids);

        //新增orderDtl
        ArrayList<OrderDtlCreate> arrayList = getOrderDtlCreates(productOfShoppingCartList, orders);
        orderDtlMapper.insertOrderDtls(arrayList);

        return shoppingCartMapper.delByShoppingCartIds(ids);
    }

    // 根据购物车中的商品列表和订单信息，创建订单详情列表
    private static ArrayList<OrderDtlCreate> getOrderDtlCreates(List<ProductOfShoppingCart> productOfShoppingCartList, Orders orders) {
        // 创建一个订单详情列表
        ArrayList<OrderDtlCreate> arrayList = new ArrayList<>();
        // 创建一个订单详情对象
        OrderDtlCreate create = new OrderDtlCreate();

        // 遍历购物车中的商品列表
        for (ProductOfShoppingCart p : productOfShoppingCartList) {

            // 设置订单详情对象的订单ID
            create.setOrderId(orders.getOrderId());
            // 设置订单详情对象的商品ID
            create.setProductId(p.getProductId());
            // 设置订单详情对象的商品名称
            create.setProductName(p.getProductName());
            // 设置订单详情对象的商品标题
            create.setProductTitle(p.getProductTitle());
            // 设置订单详情对象的商品简介
            create.setProductIntro(p.getProductIntro());
            // 设置订单详情对象的商品图片
            create.setProductPicture(p.getProductPicture());
            // 设置订单详情对象的商品价格
            create.setProductPrice(p.getProductPrice());
            // 设置订单详情对象的商品售价
            create.setProductSellingPrice(p.getProductSellingPrice());
            // 设置订单详情对象的商品数量
            create.setNum(p.getNum());
            // 将订单详情对象添加到订单详情列表中
            arrayList.add(create);
        }
        // 返回订单详情列表
        return arrayList;
    }
}

